package com.hangwei.paperless.presenter;

import android.support.v4.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.net.NetManager;
import com.twelve.all.net.CallBack;
import com.twelve.all.net.ResponseInfo;
import com.twelve.all.presenter.BasePresenter;
import com.twelve.all.utils.ToastUtil;

import java.util.List;

import rx.Observable;

/**
 * Created by 张龙臣 on 2018/10/25.
 * <p>
 * 使用的时候在界面直接 new 更加灵活
 */

public abstract class RecyclerPresenter<T> extends BasePresenter {

    public void setRecyclerListener(RecyclerListener recyclerListener) {
        mRecyclerListener = recyclerListener;
    }

    private RecyclerListener mRecyclerListener;

    public interface RecyclerListener {
        void onRefreshCompleted();
    }

    protected static  int PAGE_SIZE = 10;
    protected int mNextRequestPage = 1;
    private SwipeRefreshLayout mRefreshLayout;
    protected BaseQuickAdapter<T, BaseViewHolder> mAdapter;

    public RecyclerPresenter(SwipeRefreshLayout refreshLayout, BaseQuickAdapter<T, BaseViewHolder> adapter) {
        this.mRefreshLayout = refreshLayout;
        this.mAdapter = adapter;
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        });
        if (mRefreshLayout != null)
            mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    refresh();
                }
            });
    }

    protected abstract Observable<ResponseInfo<List<T>>> getObservable(int page);

    public void loadMore() {
        mSubscription = NetManager.getInstance().doSubscribe(new NetManager.Model<List<T>>(new CallBack<List<T>>() {
            @Override
            public void onCompleted(List<T> ts) {
                setData(false, ts);
            }

            @Override
            public void onError() {
                mAdapter.loadMoreFail();
            }
        }) {
            @Override
            protected Observable<ResponseInfo<List<T>>> getObservable() {
                return RecyclerPresenter.this.getObservable(mNextRequestPage);
            }
        });
    }

    public void refresh() {
        refresh(null);
    }

    public void refresh(final CallBack<List<T>> callBack) {
        mNextRequestPage = 1;
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        mSubscription = NetManager.getInstance().doSubscribe(new NetManager.Model<List<T>>(new CallBack<List<T>>() {
            @Override
            public void onCompleted(List<T> ts) {
                mAdapter.setEnableLoadMore(true);
                if (mRefreshLayout != null)
                    mRefreshLayout.setRefreshing(false);
                setData(true, ts);
                if (callBack != null) callBack.onCompleted(ts);
                if (mRecyclerListener != null)
                    mRecyclerListener.onRefreshCompleted();
            }

            @Override
            public void onError() {
                mAdapter.setEnableLoadMore(true);
                if (mRefreshLayout != null)
                    mRefreshLayout.setRefreshing(false);
                if (callBack != null) callBack.onError();
            }
        }) {
            @Override
            protected Observable<ResponseInfo<List<T>>> getObservable() {
                return RecyclerPresenter.this.getObservable(mNextRequestPage);
            }
        });
    }


    protected void setData(boolean isRefresh, List<T> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (mNextRequestPage == 1) {
                mAdapter.setNewData(data);
            } else if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局(    不显示且不足一屏就自动加载，page还不对 )
//            mAdapter.loadMoreEnd(!isRefresh);
            mAdapter.loadMoreEnd(false);
            if (mNextRequestPage == 1) {//不需要的提醒
                if (isRefresh) {
                    ToastUtil.showToast("数据更新成功");
                } else
                    ToastUtil.showToast("没有更多数据了");
            }
        } else {
            mAdapter.loadMoreComplete();
        }
    }
}
